<html>
<head><meta charset="utf-8"><title>Why is def_id_visibility private? · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20is.20def_id_visibility.20private.3F.html">Why is def_id_visibility private?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="199239569"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20is%20def_id_visibility%20private%3F/near/199239569" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20is.20def_id_visibility.20private.3F.html#199239569">(May 30 2020 at 05:20)</a>:</h4>
<p>Hello all, I'm trying to fix <a href="https://github.com/rust-lang/rust/issues/72769">https://github.com/rust-lang/rust/issues/72769</a> and I need to know the visibility of the linked item for that. My idea was to use <code>rustc_privacy::def_id_visibility</code> to look it up, but that function is private. Is there a reason for that, is there a better way to find that information?</p>



<a name="199246341"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20is%20def_id_visibility%20private%3F/near/199246341" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20is.20def_id_visibility.20private.3F.html#199246341">(May 30 2020 at 08:51)</a>:</h4>
<p><code>tcx.privacy_access_levels(LOCAL_CRATE).is_exported(hir_id)</code></p>



<a name="199261474"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20is%20def_id_visibility%20private%3F/near/199261474" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20is.20def_id_visibility.20private.3F.html#199261474">(May 30 2020 at 15:18)</a>:</h4>
<p>Hmm, if I try that I get this error instead:</p>
<div class="codehilite"><pre><span></span><code><span class="n">error</span><span class="p">[</span><span class="n">E0308</span><span class="p">]</span>: <span class="nc">mismatched</span><span class="w"> </span><span class="n">types</span><span class="w"></span>
<span class="w">   </span><span class="o">-</span>-&gt; <span class="nc">src</span><span class="o">/</span><span class="n">librustdoc</span><span class="o">/</span><span class="n">passes</span><span class="o">/</span><span class="n">collect_intra_doc_links</span><span class="p">.</span><span class="n">rs</span>:<span class="mi">709</span>:<span class="mi">103</span><span class="w"></span>
<span class="w">    </span><span class="o">|</span><span class="w"></span>
<span class="mi">709</span><span class="w"> </span><span class="o">|</span><span class="w">                     </span><span class="k">if</span><span class="w"> </span><span class="o">!</span><span class="bp">self</span><span class="p">.</span><span class="n">cx</span><span class="p">.</span><span class="n">tcx</span><span class="p">.</span><span class="n">privacy_access_levels</span><span class="p">(</span><span class="n">rustc_hir</span>::<span class="n">def_id</span>::<span class="n">LOCAL_CRATE</span><span class="p">).</span><span class="n">is_exported</span><span class="p">(</span><span class="n">def_id</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="o">|</span><span class="w">                                                                                                       </span><span class="o">^^^^^^</span><span class="w"> </span><span class="n">expected</span><span class="w"> </span><span class="k">struct</span> <span class="err">`</span><span class="n">rustc_hir</span>::<span class="n">HirId</span><span class="err">`</span><span class="p">,</span><span class="w"> </span><span class="n">found</span><span class="w"> </span><span class="k">struct</span> <span class="err">`</span><span class="n">rustc_span</span>::<span class="n">def_id</span>::<span class="n">DefId</span><span class="err">`</span><span class="w"></span>
</code></pre></div>



<a name="199261526"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20is%20def_id_visibility%20private%3F/near/199261526" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20is.20def_id_visibility.20private.3F.html#199261526">(May 30 2020 at 15:19)</a>:</h4>
<p>I got <code>def_id</code> from <code>Res::opt_def_id()</code>: <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/def/enum.Res.html#method.opt_def_id">https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/def/enum.Res.html#method.opt_def_id</a></p>



<a name="199261582"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20is%20def_id_visibility%20private%3F/near/199261582" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20is.20def_id_visibility.20private.3F.html#199261582">(May 30 2020 at 15:20)</a>:</h4>
<p>you can use <code>tcx.hir().as_local_hir_id(def_id.as_local()?)</code> to get the relevant <code>HirId</code></p>



<a name="199261916"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20is%20def_id_visibility%20private%3F/near/199261916" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20is.20def_id_visibility.20private.3F.html#199261916">(May 30 2020 at 15:27)</a>:</h4>
<p>You can also use <code>def_id.expect_local()</code>,  "localize" the def id or panic if the def is is foreign. You can use that if the call should never fail.</p>



<a name="199262116"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20is%20def_id_visibility%20private%3F/near/199262116" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20is.20def_id_visibility.20private.3F.html#199262116">(May 30 2020 at 15:32)</a>:</h4>
<p>I'd rather avoid <code>expect()</code> calls until I know the code base better, <code>as_local()</code> works pretty well though :)</p>
<div class="codehilite"><pre><span></span><code><span class="w">                </span><span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">local</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">res</span><span class="p">.</span><span class="n">opt_def_id</span><span class="p">().</span><span class="n">and_then</span><span class="p">(</span><span class="o">|</span><span class="n">def_id</span><span class="o">|</span><span class="w"> </span><span class="n">def_id</span><span class="p">.</span><span class="n">as_local</span><span class="p">())</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                    </span><span class="kd">let</span><span class="w"> </span><span class="n">hir_id</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">cx</span><span class="p">.</span><span class="n">tcx</span><span class="p">.</span><span class="n">hir</span><span class="p">().</span><span class="n">as_local_hir_id</span><span class="p">(</span><span class="n">local</span><span class="p">);</span><span class="w"></span>
<span class="w">                    </span><span class="k">if</span><span class="w"> </span><span class="o">!</span><span class="bp">self</span><span class="p">.</span><span class="n">cx</span><span class="p">.</span><span class="n">tcx</span><span class="p">.</span><span class="n">privacy_access_levels</span><span class="p">(</span><span class="n">rustc_hir</span>::<span class="n">def_id</span>::<span class="n">LOCAL_CRATE</span><span class="p">).</span><span class="n">is_exported</span><span class="p">(</span><span class="n">hir_id</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="o">..</span><span class="p">.</span><span class="w"></span>
</code></pre></div>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>